package com.lanou.musicproject.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import com.alibaba.fastjson.JSON;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.lanou.musicproject.R;
import com.lanou.musicproject.adapter.SeekSingerAlbumGridView;
import com.lanou.musicproject.bean.SingerAlbum;
import com.lanou.musicproject.utils.MyConfig;
import com.lanou.musicproject.volley.VolleySingleton;
import org.json.JSONObject;

import java.util.List;

/**
 * Created by dllo on 15/10/24.
 */
public class SeekSingerAlbumFragment extends Fragment {
    private GridView seeksingeralbum_gridview;
    private VolleySingleton volleySingleton;
    private String url ;
    private int _id ;
    private List<SingerAlbum.DataEntity> data;
    private SeekSingerAlbumGridView adapter;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view= inflater .inflate(R.layout.seeksingeralbun_fragment, null);
        seeksingeralbum_gridview = (GridView) view.findViewById(R.id.seeksingeralbum_gridview);
        volleySingleton = VolleySingleton.getInstance(getActivity());
        initData();
        return view;
    }

    private void initData() {
        //接收数据替换q_id
        _id = MyConfig.getSharePreInt(getActivity(), "entiey", "_id");
        url = "http://api.dongting.com/song/singer/" +
                _id +
                "/albums?page=2&utdid=VSeGyjss0fEDAOqIktP5W%2BO4&size=10";


        JsonObjectRequest jr = new JsonObjectRequest(url, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                String str = response.toString();
                SingerAlbum song = JSON.parseObject(str, SingerAlbum.class);
                data = song.getData();
                adapter = new SeekSingerAlbumGridView(getActivity(), data);
                seeksingeralbum_gridview.setAdapter(adapter);

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        volleySingleton.addQueue(jr);
    }

    }

